﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TM.Reader;
using TM.Objects;
using System.Net;
using System.IO;

namespace TM.Reader
{
    public class YahooAPI
    {
       
        public  static TMPriceBar GetHistoricalData(string symbol, DateTime LastUpdatedDate)
        {
            //http://ichart.finance.yahoo.com/table.csv?s=UNITECH.NS&d=1&e=21&f=2011&g=d&a=7&b=12&c=2009&ignore=.csv

            List<string[]> result = null;
           
            string url = "http://ichart.finance.yahoo.com/table.csv?s=" + symbol  + "&a=" + (LastUpdatedDate.Month - 1).ToString() + "&b=" + LastUpdatedDate.Day.ToString() + "&c=" + LastUpdatedDate.Year.ToString() + "&d=" + DateTime.Now.Month.ToString() + "&e=" + DateTime.Now.Day.ToString() + "&f=" + DateTime.Now.Year.ToString() + "&g=d&ignore=.csv";
            WebResponse response = null;
            StreamReader reader = null;
            HttpWebRequest request;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";
                response = request.GetResponse();
                reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);

                result = ParseCSV(reader);

               
               
            }
            catch (Exception ex)
            {
               //need to ahndle
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                if (response != null)
                    response.Close();
                request = null;
                //result = null;


            }
            return ToList(result, symbol);
           
        }

        private static List<string[]> ParseCSV(StreamReader reader)
        {
            List<string[]> parsedData = new List<string[]>();

            try
            {
                using (StreamReader readFile = reader)
                {
                    string line;
                    string[] row;
                    int i = 0;
                    while ((line = readFile.ReadLine()) != null)
                    {
                        if (i != 0)// skip first row; it is heading
                        {

                            row = line.Split(',');
                            parsedData.Add(row);
                        }
                        i++;// will be used this later for caluculation
                    }
                }
            }
            catch (Exception e)
            {
              //handle error
            }

            return parsedData;
        }
        private static TMPriceBar ToList(List<string[]> result, string symbol)
        {
            //List<PriceBarEnt> priceBars = new List<PriceBarEnt>();
            TMPriceBar priceBar = new TMPriceBar();
            try
            {
                int i = 0;
               
                foreach (string[] str in result)
                {
                    if (i > Constants.HISTORY_SIZE)
                    {
                        break;//end with 200 data
                    }

                    priceBar.Date[i] = Convert.ToDateTime(str[0].ToString());
                    priceBar.Open[i] = Convert.ToDouble(str[1].ToString());
                    priceBar.High[i] = Convert.ToDouble(str[2].ToString());
                    priceBar.Low[i] = Convert.ToDouble(str[3].ToString());
                    priceBar.Close[i] = Convert.ToDouble(str[4].ToString());
                    priceBar.Volume[i] = Convert.ToDouble(str[5].ToString());
                   // priceBars.Add(priceBar);
                    i++;
                }
            }
            catch (Exception e)
            {
              //hanmdle
            }

            return priceBar;
        }
    }
}
